今天來說明分析 Audit Log 的過程與實做。
首先從官方文件可以看到結構。
[timestamp],[serverhost],[username],[host],[connectionid],[queryid],[operation],[database],[object],[retcode]
有這些資料就能分析了(以下可能有更好的解法,這個解法只是其中一種)。
[timestamp], 分段因為檔案不是用 \r\n 或 \n 來分隔一行,而是用空白,在[timestamp] 與 [object] 也會有空白,所以需要先分段。透過 [timestamp], 來分段。
$matches = ([regex] '(\d{4})(\d{2})(\d{2}) (\d{2}):(\d{2}):(\d{2}),').Matches($auditfilecontent)
, 分欄位接下來就是每一個 , 欄位,到 [object] 前都是這樣分的。
$submatches = ([regex] ',').Matches($line)
$timestamp = $line.SubString(0, $submatches[0].Index)
$serverhost = $line.SubString($submatches[0].Index+1, $submatches[1].Index - $submatches[0].Index-1)
$username = $line.SubString($submatches[1].Index+1, $submatches[2].Index - $submatches[1].Index-1)
$hosts = $line.SubString($submatches[2].Index+1, $submatches[3].Index - $submatches[2].Index-1)
$connectionid = $line.SubString($submatches[3].Index+1, $submatches[4].Index - $submatches[3].Index-1)
$queryid = $line.SubString($submatches[4].Index+1, $submatches[5].Index - $submatches[4].Index-1)
$operation = $line.SubString($submatches[5].Index+1, $submatches[6].Index - $submatches[5].Index-1)
$database = $line.SubString($submatches[6].Index+1, $submatches[7].Index - $submatches[6].Index-1)
$retcode = $line.SubString($submatches[$submatches.Count-1].Index+1,$line.length - $submatches[$submatches.Count-1].Index-1).Trim()
到了 [object],因為有可能裡頭也有 , 所以不能直接拿 split 的值來用,取最後一個 , 之前的當作是 [object]。
if ($submatches[$submatches.Count-1].Index - $submatches[7].Index- 3 -lt 0)
{
$object = ""
}else
{
$object = $line.SubString($submatches[7].Index+2, $submatches[$submatches.Count-1].Index - $submatches[7].Index-3)
}
有了資料就能作其他用途了,明天再繼續介紹。